﻿#include "weatherwidget.h"
#include <QDebug>
#include <QStyleOption>

weatherWidget::weatherWidget(QWidget *parent) : QWidget(parent)
{
    initMemberVariables();
    initLabelData();
    initLayout();
    connectGSetting();
}

void weatherWidget::initMemberVariables()
{
    m_pWeatherIconLabel = new QLabel();
    m_pWeatherIconLabel->setFixedSize(16, 16);
    m_pWeatherIconLabel->installEventFilter(this);

    m_pAreaLabel = new QLabel();
    m_pAreaLabel->setStyleSheet("QLabel {font:12pt;}");
    m_pPictureToWhite = new PictureToWhite();
   
    m_pAreaLabel->installEventFilter(this);

    m_pMainHLayout = new QHBoxLayout();
    m_pMainHLayout->setContentsMargins(32, 0, 0, 0);
    m_pMainHLayout->setSpacing(5);
    return;

}

void weatherWidget::initLabelData()
{
    QPixmap pixmap = QIcon::fromTheme(WeatherGsetting::getInstance()->getIcon()).pixmap(QSize(m_pWeatherIconLabel->size()));
    pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
    m_pWeatherIconLabel->setPixmap(pixmap);
    setLabelData();
    return;
}

void weatherWidget::setLabelData()
{
    m_pAreaLabel->setText(WeatherGsetting::getInstance()->getWeather());
    QPalette palette = m_pAreaLabel->palette();
    QStyleOption opt;
    opt.init(m_pAreaLabel);
    QColor color = opt.palette.color(QPalette::Text);
    color.setAlphaF(0.45);
    palette.setBrush(QPalette::WindowText, color);
    m_pAreaLabel->setPalette(palette);
    this->update();
}
void weatherWidget::initLayout()
{
    m_pWeatherWidget = new QWidget(this);
    m_pWeatherWidget->setLayout(m_pMainHLayout);
    m_pMainHLayout->addWidget(m_pWeatherIconLabel);
    m_pMainHLayout->addWidget(m_pAreaLabel);
    m_pMainHLayout->addItem(new QSpacerItem(150, 10, QSizePolicy::Expanding));
    m_pWeatherWidget->installEventFilter(this);
    installEventFilter(this);
    this->setLayout(m_pMainHLayout);
    return;
}

void weatherWidget::connectGSetting()
{
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,this,&weatherWidget::styleChangeSlots);
    connect(WeatherGsetting::getInstance(),&WeatherGsetting::Sig_weatherChanged,this,&weatherWidget::weatherChangeSlots);
}

bool weatherWidget::eventFilter(QObject *obj, QEvent *event)
{
    if ((qobject_cast<QWidget*>(obj) == this || qobject_cast<QWidget*>(obj) == m_pWeatherWidget \
         || qobject_cast<QLabel*>(obj) == m_pAreaLabel || qobject_cast<QLabel*>(obj) == m_pWeatherIconLabel) \
            && event->type() == QEvent::MouseButtonRelease) {
        if(!AppManagerDbus::getInstance()->LaunchApp("kylin-weather.desktop")) {
            qWarning()<<"AppManagerDbus 接口无效，采用QProcess启动";
            QProcess p(0);
            p.startDetached("kylin-weather");
            p.waitForStarted();
        }

        return true;
    }
    return false;
}

void weatherWidget::weatherChangeSlots(QString key)
{
    if (key == WEATHER_GSETTING_KEY) {
        QPixmap pixmap = QIcon::fromTheme(WeatherGsetting::getInstance()->getIcon()).pixmap(QSize(m_pWeatherIconLabel->size()));
        pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
        m_pWeatherIconLabel->setPixmap(pixmap);
        setLabelData();
    }
    return;
}
void weatherWidget::styleChangeSlots(QString key)
{
    if (key == UKUI_STYLE_NAME_KEY) {
        QPixmap pixmap = QIcon::fromTheme(WeatherGsetting::getInstance()->getIcon()).pixmap(QSize(m_pWeatherIconLabel->size()));
        pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
        m_pWeatherIconLabel->setPixmap(pixmap);
        setLabelData();
    }
    return;
}
